//
//  jQuiz.java
//  jQuiz
//
//  Created by Justin Lazarow on 2/23/08.
//	Released Under GPLv2 Licese, read LICENSE before using
//
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
import java.beans.*;
import javax.swing.filechooser.*;
import javax.imageio.*;


public class jQuiz {

    public static void main (String args[]) {
        //Oh Boy! Here goes nothing
		QuizFrame mainFrame = new QuizFrame();
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);

    }
}


class QuizFrame extends JFrame
	{
	boolean run = false;
	String filepath;
	QuizPanel answerPanel = new QuizPanel();
	ActionListener fileaction = new FileListener();
	public QuizFrame() {
		//Let OS pick screen size/position, *cough* unlike DemiQuiz *cough*
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize(); //make new Dimenension object to manipulate users's screen
		int screenht = screenSize.height;
		int screenwd = screenSize.width;
		setSize(screenwd/2,screenht/2);
		setLocationByPlatform(true);
		Image ico = kit.getImage("images.jpeg");
		setIconImage(ico);
		setTitle("jQuiz: Civil War pre0.1");
		setResizable(true);
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		JMenuItem openMenu = new JMenuItem("Locate Quizzes");
		openMenu.addActionListener(fileaction);
		fileMenu.add(openMenu);
		JMenuItem closeMenu = new JMenuItem(exitAction);
		fileMenu.add(closeMenu);
		closeMenu.addActionListener(exitAction);
		add(answerPanel);
		}
	
	
	Action exitAction = new AbstractAction("Exit")
		{
			public void actionPerformed(ActionEvent event)
			{
				//hax
				System.exit(0);
		
			}
		
		};
	public class FileListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				JFileChooser locatefile = new JFileChooser();
				locatefile.setCurrentDirectory(new File("."));
				int fileresult = locatefile.showOpenDialog(QuizFrame.this);
				
				filepath = locatefile.getSelectedFile().getPath();
				if (run)
				{	
					try
						{
						answerPanel.redrawUI(answerPanel.makeScanner(filepath));
						}
					catch (FileNotFoundException exception)
					{
							exception.printStackTrace();
					}
				}
				else
					{
						
						run = true;
						answerPanel.drawUI(new String[6],filepath);
						run = true;
					}
				run = true;
				}
		
		}
	
		
	}
	
class QuizPanel extends JPanel
	{
			ImageComponent correctDraw = new ImageComponent("check.gif");
			ImageComponent wrongDraw = new ImageComponent("ouch.png");
			ActionListener click = new ClickListener();
			JButton[] manyButtons = new JButton[5];
			JTextField panelQuestion;
			JCheckBox chkbox;
			Scanner aParser = null;
			String quizpath;
			int inital = 0;	
			int rightAnswer = 1;
			int a = 1;
			int b = 2;
			int c = 3;
			int d = 4;
			int e = 5;
			int randomNumber;
			int theirAnswer;
		
		public QuizPanel()
		{
			setLayout(new BorderLayout());
		
			answerPanel = new JPanel();
			answerPanel.setLayout(new GridLayout(8,1));
		}
		
		
		public Scanner drawUI(String[] qArray,String quizpath) 
		{
			String parsedA = "";
			String parsedB = "";
			String parsedC = "";
			String parsedD = "";
			String parsedE = "";
			String parsedQ = "";
				try
				{
					aParser = makeScanner(quizpath);
	
					qArray = parseQuestions(22, aParser);
					qArray = scrambleAnswers(qArray);
					parsedQ = qArray[0];
					parsedA = qArray[1];
					parsedB = qArray[2];
					parsedC = qArray[3];
					parsedD = qArray[4];
					parsedE = qArray[5];
					System.out.println(qArray[0]);
		
				}
				catch (FileNotFoundException exception)
					{
						//f.printStackTrace();
					}

				panelQuestion = displayQuestion(parsedQ);
		
				manyButtons[0] = makeButton("A",parsedA);
				manyButtons[1] = makeButton("B",parsedB);
				manyButtons[2] = makeButton("C",parsedC);
				manyButtons[3] = makeButton("D",parsedD);
				manyButtons[4] = makeButton("E",parsedE);
		
				chkbox = makeChkBox("Review Mode");
		
			this.add(answerPanel, BorderLayout.WEST);
			revalidate();
			return aParser;
		
		}
		
		public void redrawUI(Scanner parser)
		{
					aParser = parser;
					String parsedA = "";
					String parsedB = "";
					String parsedC = "";
					String parsedD = "";
					String parsedE = "";
					String parsedQ = "";
	
					String[] qArray = parseQuestions(0,parser);
					qArray = scrambleAnswers(qArray);
					parsedQ = qArray[0];
					parsedA = qArray[1];
					parsedB = qArray[2];
					parsedC = qArray[3];
					parsedD = qArray[4];
					parsedE = qArray[5];
					
					panelQuestion = editQuestion(parsedQ);
					manyButtons[0] = editButton("A",parsedA,0);
					manyButtons[1] = editButton("B",parsedB,1);
					manyButtons[2] = editButton("C",parsedC,2);
					manyButtons[3] = editButton("D",parsedD,3);
					manyButtons[4] = editButton("E",parsedE,4);
					chkbox = editChkBox("Review Mode");
					revalidate();
		}
		
		
		public Scanner makeScanner(String quizfile) throws FileNotFoundException
		{
			Scanner in = new Scanner(new File(quizfile),"UTF8");
				
			return in;
		}
		
		
		private JButton makeButton(String label, String parsed)
		{
			JButton button = new JButton(label+ " "+parsed);
			button.addActionListener(click);
			answerPanel.add(button);
			return button;
		}
		
		private JButton editButton(String label, String parsed, int remove)
		{
			
			answerPanel.remove(manyButtons[remove]);
			JButton button = new JButton(label+" "+parsed);
			button.addActionListener(click);
			answerPanel.add(button);
			return button;
			
		}
		
		
		private JCheckBox makeChkBox(String label2)
		{
			JCheckBox chkbox = new JCheckBox(label2);
			answerPanel.add(chkbox);
			return chkbox;
		}
		
		private JCheckBox editChkBox(String label2)
		{
			answerPanel.remove(chkbox);
			JCheckBox chkbox = new JCheckBox(label2);
			answerPanel.add(chkbox);
			return chkbox;
		}
		

		private JTextField displayQuestion(String question)
		{
			JTextField questionField = new JTextField(question.length());
			questionField.setText(question);
			questionField.setEditable(false);
			answerPanel.add(questionField);
			return questionField;
		}
		
		private JTextField editQuestion(String question)
		{
			answerPanel.remove(panelQuestion);
			JTextField questionField = new JTextField(question.length());
			questionField.setText(question);
			questionField.setEditable(false);
			answerPanel.add(questionField);
			return questionField;
		}
		
		
		public String[] parseQuestions(int id, Scanner in)
		{
			in.useDelimiter("\t");
			in.nextLine();
			in.next();
			in.next();
			in.next();
			String[] Questions = {in.next(),in.next(),in.next(),in.next(),in.next(),in.next()};
			System.out.println(Questions[5]);
			return Questions;
		}
		
		public String[] scrambleAnswers(String[] qArray)
		{
			a = 1;
			b = 2;
			c = 3;
			d = 4;
			e = 5;
			rightAnswer = 1;
			Random random = new Random();
			randomNumber = random.nextInt(5) + 1;
			System.out.println(randomNumber);
			a = randomNumber;
			rightAnswer = a;
			b = a;
			while (b == a)
			{
				b = random.nextInt(5) + 1;
				System.out.println(b);
			}
			c = b;
			while (c == a || c == b)
			{
			
				c = random.nextInt(5) + 1;
				System.out.println(c);
			}
			
			d = c;
			while (d == a || d == b || d == c)
			{
			
				d = random.nextInt(5) + 1;
				System.out.println(d);
			}
			e = d;
			while (e == a || e == b || e == c || e == d)
			{
			
				e = random.nextInt(5) + 1;
				System.out.println(e);
			}
				//Now we have to make switch around the new array
				String temp1 = qArray[1];
				String temp2 = qArray[2];
				String temp3 = qArray[3];
				String temp4 = qArray[4];
				String temp5 = qArray[5];
				qArray[a] = temp1;
				qArray[b] = temp2;
				qArray[c] = temp3;
				qArray[d] = temp4;
				qArray[e] = temp5;
				return qArray;
		}
	
		
		
		private class ClickListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				String answer = event.getActionCommand();
				System.out.println(answer);
				String choice = answer.substring(0,1);
				System.out.println(choice);
				if ( choice.equals("A"))
						theirAnswer = 1;
				if ( choice.equals("B"))
						theirAnswer = 2;
				if ( choice.equals("C"))
						theirAnswer = 3;
				if ( choice.equals("D"))
						theirAnswer = 4;
				if ( choice.equals("E"))
						theirAnswer = 5;
				System.out.println(theirAnswer + " " + rightAnswer);		
				if (theirAnswer == rightAnswer)
					{
						answerPanel.remove(wrongDraw);
						answerPanel.add(correctDraw);
					}
				else 
					{

						answerPanel.remove(correctDraw);
						answerPanel.add(wrongDraw);
					}
				
				redrawUI(aParser);
			}
		}
		
		JPanel answerPanel;
		
	}
	
	
	
	class ImageComponent extends JComponent 
	{
	
		public ImageComponent(String filename)
		{
			try
			{
				img = ImageIO.read(new File(filename));
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		public void paintComponent(Graphics g)
		{
		
			if (img == null)
				return;
			g.drawImage(img,0,0,null);
		}
		private Image img;
	}
	
	
	
	
